package com.example.publishsubscribepattern.subject;

import com.example.publishsubscribepattern.observer.Observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ash
 * @description 抽象通知者
 * @create 2023/2/14  22:55:38
 * @className Subject
 * @verson 1.0.0
 */
public abstract class Subject {

    private final List<Observer> observers = new ArrayList<>();

    /**
     * 添加观察者
     *
     * @param observer
     */
    public void attach(Observer observer) {
        observers.add(observer);
    }

    /**
     * 删除观察者
     *
     * @param observer
     */
    public void detach(Observer observer) {
        observers.remove(observer);
    }

    /**
     * 通知
     */
    public void notifyMessage(){
        observers.forEach(Observer::update);
    }

}
